package cn.springboot.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

/**
 * @author liufl E-mail:Huberier@allpyra.com
 * @version 创建时间：2017年8月24日 下午4:06:46 类说明
 */
public class DateUtil {
	static SimpleDateFormat standardFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public static Date add(Date date, int calendarField, int amount) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		} else {
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(calendarField, amount);
			return c.getTime();
		}
	}

	/**
	 * 获取星期几
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeekDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		return dayOfWeek - 1;
	}

	/**
	 * 把日期字符串格式化成日期类型
	 * 
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date str2Date(String dateStr) {
		return str2Date(dateStr, null);
	}

	public static Date str2Date(String dateStr, String format) {
		SimpleDateFormat sdf = format == null ? standardFormat
				: new SimpleDateFormat(format);
		try {
			sdf.setLenient(false);
			return sdf.parse(dateStr);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 把日期类型格式化成字符串
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String date2String(Date date) {
		return date2String(date, null);
	}

	public static String date2String(Date date, String format) {
		date = date==null?new Date():date;
		SimpleDateFormat sdf = format == null ? standardFormat
				: new SimpleDateFormat(format);
		try {
			return sdf.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	public static String timestamp2str(Timestamp ts) {
		try {
			String str = standardFormat.format(ts);
			return str;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static Timestamp str2timestamp(String str) {
		if(StringUtils.isBlank(str)){
			return new Timestamp(new Date().getTime());
		}
		try {
			Timestamp timestamp = Timestamp.valueOf(str);
			return timestamp;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 天数差
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int diffDay(Date before, Date after) {
		return Integer.parseInt(String.valueOf(((after.getTime() - before
				.getTime()) / 86400000)));
	}

	/**
	 * 分钟差
	 * 
	 * @param before
	 * @param after
	 * @return
	 */
	public static int diffMinute(Date before, Date after) {
		return (int) (after.getTime() - before.getTime()) / 1000 / 60;
	}

	/**
	 * 获取指定时间
	 * @param date
	 * @param hour
	 * @param minute
	 * @param million
	 * @return
	 */
	public static String getAppointTime(Date date, int hour, int minute,
			int million) {
		if (date == null) {
			date = new Date();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// cal.set(Calendar.DAY_OF_MONTH,1);
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE, minute);
		cal.set(Calendar.SECOND, million);
		return date2String(cal.getTime());
	}

	/**
	 * 获取指定时间
	 * @param date
	 * @param hour
	 * @param minute
	 * @param million
	 * @return
	 */
	public static String getAppointDelayTime(Date date, int delayHour, int delayMinute,
			int delayMillion) {
		if (date == null) {
			date = new Date();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// cal.set(Calendar.DAY_OF_MONTH,1);
		cal.add(Calendar.HOUR_OF_DAY, delayHour);
		cal.add(Calendar.MINUTE, delayMinute);
		cal.add(Calendar.SECOND, delayMillion);
		return date2String(cal.getTime());
	}

	/**
	 * 获取本周一日期
	 * 
	 * @return
	 */
	public static String getWeekBegin() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		String time = getAppointTime(cal.getTime(),0,0,0);
		return time;
	}

	/**
	 * 获取本周日日期
	 * 
	 * @return
	 */
	public static String getWeekEnd() {
		Calendar cal = Calendar.getInstance();
		// 这种输出的是上个星期周日的日期，因为老外那边把周日当成第一天
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

		// 增加一个星期，才是我们中国人理解的本周日的日期
		cal.add(Calendar.WEEK_OF_YEAR, 1);
		String time = getAppointTime(cal.getTime(),23,59,59);
		return time;
	}
	
	/**
	 * 获取指定月初
	 * @param date
	 * @return
	 */
	public static String getMonthBegin(Date date){
		if (date == null) {
			date = new Date();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		String time = getAppointTime(cal.getTime(),0,0,0);
		return time;
	}
	
	/**
	 * 获取指定月末
	 * @param date
	 * @return
	 */
	public static String getMonthEnd(Date date){
		if (date == null) {
			date = new Date();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		String time = getAppointTime(cal.getTime(),23,59,59);
		return time;
	}
	
	public static void main(String[] args) {
		System.out.println(getAppointTime(new Date(), 22, 10, 23));
		System.out.println(getWeekBegin());
		System.out.println(getWeekEnd());
		
		System.out.println(getMonthBegin(null));
		System.out.println(getMonthEnd(null));
	}
}
